package com.encryption.dao;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.logic.impl.ConditionSqlParser;
import com.encryption.model.ManageActionLog;

/**
 * A data access object (DAO) providing persistence and search support for
 * ManageActionLog entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.encryption.model.ManageActionLog
 * @author MyEclipse Persistence Tools
 */

public class ManageActionLogDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ManageActionLogDAO.class);
	// property constants
	public static final String ACTION_ID = "actionId";
	public static final String NOTE = "note";
	
	public static ManageActionLogDAO instance = new ManageActionLogDAO();

	public void save(ManageActionLog transientInstance) {
		log.debug("saving ManageActionLog instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ManageActionLog persistentInstance) {
		log.debug("deleting ManageActionLog instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ManageActionLog findById(java.lang.Integer id) {
		log.debug("getting ManageActionLog instance with id: " + id);
		try {
			ManageActionLog instance = (ManageActionLog) getSession().get(
					"com.encryption.model.ManageActionLog", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ManageActionLog instance) {
		log.debug("finding ManageActionLog instance by example");
		try {
			List results = getSession()
					.createCriteria("com.encryption.model.ManageActionLog")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ManageActionLog instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from ManageActionLog as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByActionId(Object actionId) {
		return findByProperty(ACTION_ID, actionId);
	}

	public List findByNote(Object note) {
		return findByProperty(NOTE, note);
	}

	public List findAll() {
		log.debug("finding all ManageActionLog instances");
		try {
			String queryString = "from ManageActionLog";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ManageActionLog merge(ManageActionLog detachedInstance) {
		log.debug("merging ManageActionLog instance");
		try {
			ManageActionLog result = (ManageActionLog) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ManageActionLog instance) {
		log.debug("attaching dirty ManageActionLog instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ManageActionLog instance) {
		log.debug("attaching clean ManageActionLog instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * 分页查询文件日志表
	 */
	public List<ManageActionLog> findPagingByCondition(List<Condition> conditions, int page, int pageSize) {
		try {
			int startIndex = page * pageSize;
			String condsql = ConditionSqlParser.parse(conditions);
			
			String sql = "select * from manage_action_log where 1=1 " + condsql + "limit "+ startIndex +", "+ pageSize; 
			
			Query query = getSession().createSQLQuery(sql).addEntity(ManageActionLog.class);
			List<ManageActionLog> results = query.list();
			
			log.debug("执行语句:" + sql);
			return results;
		} catch (RuntimeException re) {
			log.error("findPagingByCondition failed", re);
			throw re;
		}
	}
	
	/**
	 * 分页查询文件表
	 */
	public int findTotalCountByCondition(List<Condition> conditions) {
		try {
			String condsql = ConditionSqlParser.parse(conditions);
			
			String sql = "select count(*) from manage_action_log where 1=1 " + condsql;
			
			Query query = getSession().createSQLQuery(sql);
			int totalCount = ((BigInteger) query.list().get(0)).intValue();
			
			log.debug("执行语句:" + sql);
			return totalCount;
		} catch (RuntimeException re) {
			log.error("findTotalCountByCondition failed", re);
			throw re;
		}
	}
	
	public static ManageActionLogDAO getInstance(){
		return instance;
	}
}